package axdoc.dao.base;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class BusinessEventDao {

	private static Logger logger = LoggerFactory.getLogger(BusinessEventDao.class);

	JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	/**
	 * 查询所有业务类型
	 * 
	 * @return
	 */
	public List<BusinessTypeConfig> queryBusinessTypes() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * from table_business_type_config ");
		List<BusinessTypeConfig> list = jdbcTemplate.query(sql.toString(), new BusinessTypeMapper());
		logger.info("[queryBusinessTypes][查询业务类型][sql:" + sql.toString() + "]");
		return list;
	}

	public List<EventType> queryEventTypes(int businessId) {
		String sql = "SELECT * from table_business_event where business_id=? ";
		logger.info("[queryBusinessTypes][查询事件类型][sql:" + sql + "][businessId:" + businessId + "]");
		return jdbcTemplate.query(sql, new Object[] { businessId }, new EventTypeMapper());
	}

	/**
	 * 添加业务类型
	 * 
	 * @param bt
	 * @return
	 */
	public int addBusinessType(BusinessTypeConfig bt) {
		String sql = "insert into table_business_type_config(business_id,business_name,url_type,ct) values(?,?,?,now())";
		return jdbcTemplate.update(sql.toString(),
				new Object[] { bt.getBusinessId(), bt.getBusinessName(), bt.getUrlType() });
	}

	/**
	 * 校验ID是否存在
	 * 
	 * @param id
	 * @return
	 */
	public int checkBtId(int id) {
		String sql = "select count(1) from table_business_type_config where business_id=? ";
		return jdbcTemplate.queryForObject(sql.toString(), new Object[] { id }, Integer.class);
	}

	/**
	 * 根据ID查询业务类型
	 * 
	 * @param id
	 * @return
	 */
	public BusinessTypeConfig queryBusinessTypeById(int id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * from table_business_type_config where business_id=?");
		List<BusinessTypeConfig> list = jdbcTemplate.query(sql.toString(), new Object[] { id },
				new BusinessTypeMapper());
		logger.info("[queryBusinessTypeById][根据ID查询业务类型][sql:" + sql.toString() + "]");
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}

class BusinessTypeMapper implements RowMapper<BusinessTypeConfig> {
	@Override
	public BusinessTypeConfig mapRow(ResultSet rs, int index) throws SQLException {
		BusinessTypeConfig bt = new BusinessTypeConfig();
		bt.setBusinessId(rs.getInt("business_id"));
		bt.setBusinessName(rs.getString("business_name"));
		bt.setUrlType(rs.getString("url_type"));
		return bt;
	}
}

class EventTypeMapper implements RowMapper<EventType> {
	@Override
	public EventType mapRow(ResultSet rs, int index) throws SQLException {
		EventType bt = new EventType();
		bt.setEventType(rs.getInt("event_id"));
		bt.setEventName(rs.getString("event_name"));
		bt.setBusinessType(rs.getInt("business_id"));
		return bt;
	}
}
